package GUI.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import code.HAOJianguo.ContractNetModelisation;
import code.HAOJianguo.DelegationModel;
import code.Najib.ActivityAgentNetDiscovery;
import code.Najib.CaseAgentNetDiscovery;

import app.PN.PetriNet;
import app.PN.PetriNetLibrary;
import app.model_package.LogModel;
import app.model_package.ProcessFactory;
import app.model_package.ProtocolFactory;

import GUI.view.outlook.FreeOutlookBar;
import GUI.view.outlook.FreeOutlookPane;
import client.domainModels.NodeImpl.LogFile;

public class CommandFactory {

	private final FreeOutlookBar bar;
	private FreeOutlookPane pane;
	private JPanel mainPane;
	
	private final static PetriNetLibrary pnl = new PetriNetLibrary();


	private String TinaPath ="C:\\tina-2.10.0\\bin\\";
	private String tmpDir ="";
	
	JFileChooser fc = new JFileChooser();
	JPanel panelBlanc = new JPanel();


	public CommandFactory(final FreeOutlookBar bar) {
		// System.out.println("init command factory");
		this.bar = bar;
		init();
	}

	private void init() {
		pane = this.bar.getFreeOutlookPane();
		mainPane = pane.getMainPane();
	}

	public void action(final String command) {

		if (command.equals("Open a XML file")) {
			action_1();
		} else if (command.equals("Create a XML file")) {
			action_2();
		} else if (command.equals("Process Discovery")) {
			action_3();
		} else if (command.equals("Protocol Discovery")) {
			action_4();
		} else if (command.equals("CaseAgent")) {
			action_5();
		} else if (command.equals("ActivityAgent")) {
			action_6();
		} else if (command.equals("Delegation")) {
			action_7();
		} else if (command.equals("ContractNet")) {
			action_8();
		} else if (command.equals("Process")) {
			action_9();
		} else if (command.equals("Protocol")) {
			action_10();
		} else if (command.equals("Case")) {
			action_11();
		} else if (command.equals("Activity")) {
			action_12();
		} else if (command.equals("DelegationView")) {
			action_13();
		} else if (command.equals("Contract")) {
			action_14();
		}
	}

	public void action_1() {

		final JPanel buttonpanel = new JPanel();
		final JButton button = new JButton("Open");
		buttonpanel.add(button);
		mainPane.removeAll();
		mainPane.add(buttonpanel, BorderLayout.CENTER);
		mainPane.updateUI();
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent e) {
				// TODO Auto-generated method stub
				final int returnVal = fc.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					final File file = fc.getSelectedFile();
					final LogFile log = LogToMemory.logToMemoryAndDisplay(file);
					final JPanel panel = new TableJPanel(log);
					mainPane.removeAll();
					mainPane.add(panel, BorderLayout.CENTER);
					mainPane.updateUI();
				}
			}
		});

	}

	public void action_2() {
		mainPane.removeAll();
		mainPane.add(panelBlanc, BorderLayout.CENTER);
		final JPanel panel = new FileManager();
		mainPane.removeAll();
		mainPane.add(panel, BorderLayout.CENTER);
		mainPane.updateUI();
	}

	public void action_3() {

		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire
						
						LogModel lm = log.toLogModel();

						pnl.add(ProcessFactory.createPN(lm),"process");
						

                                                final Object[] options = { "OK"};
						final int n = JOptionPane.showOptionDialog(mainPane,
								"Process Discovery has finished", "button ",
								JOptionPane.OK_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
						// /////////////////

                                                
					}
					
				};
				panel = new TableJPanel(log, "Process discovery", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}

	}

	public void action_4() {
		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						LogModel lm = log.toLogModel();						
						pnl.add(ProtocolFactory.createPN(lm),"protocol");
					
                                                final Object[] options = { "OK"};
						final int n = JOptionPane.showOptionDialog(mainPane,
								"Protocol Discovery has finished", "button ",
								JOptionPane.OK_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
						// /////////////////

                                                

					}
				};
				panel = new TableJPanel(log, "Protocol discovery", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}

	}

	public void action_5() {
		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire
						CaseAgentNetDiscovery.caseAgentNet(log);
						// for test// a supprimer ////////
						final Object[] options = { "OK" };
						final int n = JOptionPane.showOptionDialog(mainPane,
								"Case agent Network Discovery has finished", "button ",
								JOptionPane.OK_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
						// /////////////////

					}
				};
				panel = new TableJPanel(log, "Case Agent Net Discovery", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}
	}

	public void action_6() {
		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire

						// create a new windows!!!!!!! for choosing performatif.
						ActivityAgentNetDiscovery.getActivityAgentNet(log);
						// for test// a supprimer ////////
						final Object[] options = { "OK"};
						final int n = JOptionPane.showOptionDialog(mainPane,
								"Activity Agents Network Discovery has finished", "button ",
								JOptionPane.OK_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
						// /////////////////

					}
				};
				panel = new TableJPanel(log, "Activity Agent Net Discovery", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}
	}

	public void action_7() {
		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire

						// create a new windows for choosing performatif.
						DelegationModel.analyseProposeInteraction(log);
						
						// for test// a supprimer ////////
						final Object[] options = { "OK"};
						final int n = JOptionPane.showOptionDialog(mainPane,
								"Delegation Network Discovery has finished", "button ",
								JOptionPane.OK_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
						// /////////////////

					}
				};
				panel = new TableJPanel(log, "Delegation Model Discovery", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}
	}

	public void action_8() {
		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire
						// create a new windows for choosing performatif.
						ContractNetModelisation
						.analyseContractNetInteraction(log);
						// for test// a supprimer ////////
						final Object[] options = { "OK" };
						final int n = JOptionPane.showOptionDialog(mainPane,
								"Contract Network Discovery has finished", "button ",
								JOptionPane.OK_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
						// /////////////////

					}
				};
				panel = new TableJPanel(log, "Contract Net Discovery", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}
	}

	public void action_9() {
		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire

						String to_file = tmpDir+"processPN.pnml";
						String to_exec = TinaPath +"nd"; 

						pnl.get("process").toPNML(to_file);
						
						try {
							Process child = Runtime.getRuntime().exec(to_exec+" "+to_file);
						} catch (IOException ex) {
							// TODO Auto-generated catch block
							ex.printStackTrace();
						}
						
					}
				};
				panel = new TableJPanel(log, "Process Visualization", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}
	}

	public void action_10() {

		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire
						String to_file = tmpDir+"processPN.pnml";
						String to_exec = TinaPath +"nd"; 

						pnl.get("protocol").toPNML(to_file);
						
						try {
							Process child = Runtime.getRuntime().exec(to_exec+" "+to_file);
						} catch (IOException ex) {
							// TODO Auto-generated catch block
							ex.printStackTrace();
						}

					}
				};
				panel = new TableJPanel(log, "Protocol Visualization", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}

	}

	public void action_11() {

		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire
						try {
							Runtime.getRuntime()
									.exec("C:\\Program Files\\Analytic Technologies\\netdraw.exe batch C:\\Users\\NAJIB\\Documents\\NetBeansProjects\\WDT-2\\batch_case.txt");
						} catch (Exception e1) {
							System.err.println(e1);
						}
						
					}
				};
				panel = new TableJPanel(log, "Case Agent Net Visualization", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}

	}

	public void action_12() {

		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire
						try {
							Runtime.getRuntime()
									.exec("C:\\Program Files\\Analytic Technologies\\netdraw.exe batch C:\\Users\\NAJIB\\Documents\\NetBeansProjects\\WDT-2\\batch_Activity.txt");
						} catch (Exception e1) {
							System.err.println(e1);
						}
						

					}
				};
				panel = new TableJPanel(log, "Activity Net Visualization", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}

	}

	public void action_13() {
		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire
						try {
							Runtime.getRuntime()
									.exec("C:\\Program Files\\Analytic Technologies\\netdraw.exe batch C:\\Users\\NAJIB\\Documents\\NetBeansProjects\\WDT-2\\batch_propose.txt");
						} catch (Exception e1) {
							System.err.println(e1);
						}
					
					}
				};
				panel = new TableJPanel(log, "Delegation Visualization", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}

	}

	public void action_14() {

		if (mainPane.getComponentCount() == 1) {
			if (mainPane.getComponent(0) instanceof TableJPanel) {
				TableJPanel panel = (TableJPanel) mainPane.getComponent(0);
				final LogFile log = panel.getLogFile();
				final ActionListener al = new ActionListener() {
					@Override
					public void actionPerformed(final ActionEvent e) {
						// TODO Auto-generated method stub
						// A faire
						try {
							Runtime.getRuntime()
									.exec("C:\\Program Files\\Analytic Technologies\\netdraw.exe batch C:\\Users\\NAJIB\\Documents\\NetBeansProjects\\WDT-2\\batch_contract.txt");
						} catch (Exception e1) {
							System.err.println(e1);
						}
						
					}
				};
				panel = new TableJPanel(log, "Contract Net Visualization", al);
				mainPane.removeAll();
				mainPane.add(panel, BorderLayout.CENTER);
				mainPane.updateUI();

			} else {
				final Object[] options = { "OK", "Cancel" };
				final int n = JOptionPane
						.showOptionDialog(
								mainPane,
								"This fonction is available after you choose a file xml!",
								"choose a file", JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE, null, // do not use
																	// a custom
																	// Icon
								options, // the titles of buttons
								options[0]); // default button title
			}
		} else {
			System.out.println("mainpane contains nothing");
		}

	}
}
